package org.argetr.resim;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.argetr.resim.ui.model.ComponentItem;

public class ComponentTree{
  
  private static Hashtable<String, ComponentCollection> cvAllComponents = new Hashtable<String, ComponentCollection>();

  public static void AddComponentItem(String GroupName,ComponentItem item)
  {
	 if(!cvAllComponents.containsKey(GroupName))
	 {
		 //Grup yeni eklenecekse ComponentCollection da yeni olu�turulmal�
		 cvAllComponents.put(GroupName, new ComponentCollection(GroupName));
	 } 
	 cvAllComponents.get(GroupName).AddComponentItem(item);
  }	
  
  public static Iterable<String> iterateGroups()
  {	   
	  return new Iterable<String>()
	  {
		public Iterator<String> iterator() {
			return cvAllComponents.keySet().iterator();  
		}		  
	  };
  }
  
  public static ComponentCollection GetComponentCollectionByGroupName(String GroupName)
  {
	if(cvAllComponents.containsKey(GroupName))
	{
		return cvAllComponents.get(GroupName);
	}
	else 
	  throw new InvalidParameterException("Invalid group name : "+GroupName);		
  }
}

class ComponentCollection implements Iterable<ComponentItem>
{
	private String cvGroupName;
	private List<ComponentItem> cvComponentList;
	public ComponentCollection(String GroupName)
	{
		this.cvGroupName = GroupName;
		this.cvComponentList = new ArrayList<ComponentItem>(0);
	}
	
	public String GetGroupName(){ return cvGroupName; }
	
	public void AddComponentItem(ComponentItem item)
	{
		cvComponentList.add(item);
	}	

	public Iterator<ComponentItem> iterator() {	
		return this.cvComponentList.iterator();
	}
}

